<template>
  <div class="type-nav">
    <div class="container">
      <!-- 需要把三级分类与h2外层包裹一个div：作为他们两者父盒子，利用事件委派实现 -->
      <div @mouseleave="leaveShow">
        <h2 class="all" @mouseenter="enterShow">全部商品分类</h2>
        <transition name="sort">
          <div class="sort" v-show="show">
            <div class="all-sort-list2" @click="goSearch1">
              <div
                class="item"
                v-for="(c1, index) in category"
                :key="c1.categoryId"
              >
                <!-- 添加类名 -->
                <h3
                  @mouseenter="enterHandler(index)"
                  :class="{ show: currentIndex == index }"
                >
                  <a
                    :data-categoryName="c1.categoryName"
                    :data-category1Id="c1.categoryId"
                    >{{ c1.categoryName }}{{ index }}</a
                  >
                </h3>
                <div
                  class="item-list clearfix"
                  :style="{ display: currentIndex == index ? 'block' : 'none' }"
                >
                  <!-- 二级分类 -->
                  <div
                    class="subitem"
                    v-for="(c2, index) in c1.categoryChild"
                    :key="c2.categoryId"
                  >
                    <dl class="fore">
                      <dt>
                        <a
                          :data-categoryName="c2.categoryName"
                          :data-category2Id="c2.categoryId"
                          >{{ c2.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em
                          v-for="(c3, index) in c2.categoryChild"
                          :key="c3.categoryId"
                        >
                          <a
                            :data-categoryName="c3.categoryName"
                            :data-category3Id="c3.categoryId"
                            >{{ c3.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
//引入辅助函数mapState
import { mapState } from "vuex";
//按需引入,不需要的不引入
import throttle from "lodash/throttle";
export default {
  name: "TypeNav",
  //组件响应式数据
  data() {
    return {
      //当前鼠标移入进来的这个元素的索引值
      currentIndex: -1, //因为起始是没有任何一个H3有背景颜色
      show:true
    };
  },
  mounted() {
    //请求不在组件内部发,在vuex的action里面书写
    // this.$store.dispatch("categoryList");
    //通过路由信息判断当前页面是home 还是 search
    if (this.$route.path !="/home") {
      this.show = false
    }
  },
  //计算属性
  computed: {
    //可以书写数组的写法，但是需要注意，书写的是大仓库里面key
    // ...mapState(['home']),
    ...mapState({
      category: (state) => state.home.category,
    }),
  },
  methods: {
    //鼠标移入事件
    enterHandler: throttle(function (index) {
      //当前组件响应式数据存储当前鼠标进入的元素的索引值
      this.currentIndex = index;
    }, 20),
    goSearch1(event) {
      //获取触发这个事件的元素
      let targetElement = event.target;
      //获取触发这个事件的自定义属性与属性值
      let { categoryname, category1id, category2id, category3id } =
        targetElement.dataset;
      // 判断点击的是不是a标签
      if (categoryname) {
        //进入到这个if中 就说明 点击的是a标签
        //准备一个对象  存放search的参数
        let location = { name: "search" };
        let query = { categoryName: categoryname };
        //判断 进入下方if 就一定是a标签
        if (category1id) {
          //一定是一级分类的a
          query.category1Id = category1id;
        } else if (category2id) {
          ////一定是二级分类的a
          query.category2Id = category2id;
        } else {
          //一定是a，且三级分类的a
          query.category3Id = category3id;
        }
        //给location动态添加属性
        location.query = query;
        //如果在跳转路由之前有params,需要带上
        if (this.$route.params.keyword) {
          location.params = this.$route.params
        }
        //路由跳转
        this.$router.push(location);
      }
    },
    //全部商品分类鼠标进入事件
    enterShow() {
      this.show = true;
    },
    //全部商品分类鼠标移出事件
    leaveShow(){
      //当前的索引值-1
      this.currentIndex = -1;
      //隐藏三级分类:只有search模块三级才隐藏
      if (this.$route.path !="/home") {
        this.show = false;
      }
    }
  },
};
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          h3.show {
            background: red;
            a {
              color: white;
            }
          }
        }
      }
    }
  }
}
</style>
